﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.BGM;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Mvc;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.SYS;

namespace KPIS.GERP.WCF.GBMS
{
    [ServiceContract]
    public interface IMasBudgetSourceService
    {
        [OperationContract]
        List<SelectListItem> SelectList(MasBudgetSource criteriaModel = null);

        [OperationContract]
        IEnumerable<MasBudgetSource> Select(MasBudgetSource criteriaModel = null, List<string> includeSubObjectString = null);
    }

	[ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    public class MasBudgetSourceService : IMasBudgetSourceService
    {
        public IEnumerable<MasBudgetSource> Select(MasBudgetSource criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasBudgetSource> query = context.MasBudgetSources;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<SelectListItem> SelectList(MasBudgetSource criteriaModel = null)
        {
            var data = new List<SelectListItem>();
            data.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var db = new GERPContext(false))
                {
                    IQueryable<MasBudgetSource> query = db.MasBudgetSources;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var models = query.Where(m => m.RecordStatus != RecordStatus.DeletedStatus).ToList();

                    foreach (var model in models)
                    {
                        var item = new SelectListItem { Text = model.Name, Value = model.BgSourceId.ToString() };
                        //if (model.LookUpTypeId == LookUpTypeId) item.Selected = true;
                        data.Add(item);
                    }

                    return data;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
